noinst_LTLIBRARIES = libdict_backend.la

module_dictdir = $(moduledir)/dict
dict_drivers = @dict_drivers@

AM_CPPFLAGS = \
	-I$(top_srcdir)/src/lib \
	-I$(top_srcdir)/src/lib-test \
	-I$(top_srcdir)/src/lib-dict \
	-I$(top_srcdir)/src/lib-ldap \
	-I$(top_srcdir)/src/lib-sql \
	-I$(top_srcdir)/src/lib-settings \
	-I$(top_srcdir)/src/lib-var-expand \
	$(SQL_CFLAGS)

NOPLUGIN_LDFLAGS =

ldap_sources = \
	dict-ldap.c \
	dict-ldap-settings.c

libdict_backend_la_SOURCES = \
	dict-cdb.c \
	dict-sql.c \
	dict-sql-settings.c \
	$(ldap_sources)
libdict_backend_la_LIBADD =

BUILT_SOURCES = \
	dict-drivers-register.c

DISTCLEANFILES = \
	dict-drivers-register.c

nodist_libdict_backend_la_SOURCES = \
	dict-drivers-register.c

noinst_HEADERS = \
	dict-ldap-settings.h \
	dict-sql.h \
	dict-sql-private.h \
	dict-sql-settings.h

if LDAP_PLUGIN
LIBDICT_LDAP = libdict_ldap.la
libdict_ldap_la_DEPENDENCIES = $(LIBDOVECOT_LDAP) $(LIBDOVECOT_DEPS)
libdict_ldap_la_LDFLAGS = -module -avoid-version
libdict_ldap_la_LIBADD = $(LIBDOVECOT_LDAP) $(LIBDOVECOT)
libdict_ldap_la_CPPFLAGS = $(AM_CPPFLAGS) -DPLUGIN_BUILD
libdict_ldap_la_SOURCES = $(ldap_sources)
else
if HAVE_LDAP
libdict_backend_la_LIBADD += ../lib-ldap/libdldap.la
dict_drivers += ldap
endif
endif

module_dict_LTLIBRARIES = \
	$(LIBDICT_LDAP)

dict-drivers-register.c: Makefile $(top_builddir)/config.h
	$(AM_V_GEN)rm -f $@; \
	echo '/* this file automatically generated by Makefile */' >$@; \
	echo '#include "lib.h"' >>$@; \
	echo '#include "dict.h"' >>$@; \
	echo '#include "ldap-client.h"' >>$@; \
	echo '#include "dict-sql.h"' >>$@; \
	for i in $(dict_drivers) null; do \
	  if [ "$${i}" != "null" ]; then \
	        echo "extern struct dict dict_driver_$${i};" >>$@ ; \
	  fi; \
	done; \
	echo 'void dict_drivers_register_all(void) {' >>$@; \
	echo 'dict_drivers_register_builtin();' >>$@; \
	echo 'dict_sql_register();' >>$@; \
	for i in $(dict_drivers) null; do \
	  if [ "$${i}" != "null" ]; then \
	        echo "dict_driver_register(&dict_driver_$${i});" >>$@ ; \
	  fi; \
	done; \
	echo '}' >>$@; \
	echo 'void dict_drivers_unregister_all(void) {' >>$@; \
	echo '#ifdef BUILTIN_LDAP' >>$@; \
	echo 'ldap_clients_cleanup();' >>$@; \
	echo '#endif' >>$@; \
	echo 'dict_drivers_unregister_builtin();' >>$@; \
	echo 'dict_sql_unregister();' >>$@; \
	for i in $(dict_drivers) null; do \
	  if [ "$${i}" != "null" ]; then \
	        echo "dict_driver_unregister(&dict_driver_$${i});" >>$@ ; \
	  fi; \
	done; \
	echo '}' >>$@

test_programs = \
	test-dict-sql

noinst_PROGRAMS = $(test_programs)

test_dict_sql_CFLAGS = $(AM_CPPFLAGS) -DDICT_SRC_DIR=\"$(top_srcdir)/src/lib-dict-backend\"
test_dict_sql_SOURCES = \
	test-dict-sql.c
test_dict_sql_LDADD = \
	$(noinst_LTLIBRARIES) \
	$(DICT_LIBS) \
	../lib-sql/libdriver_test.la \
	../lib-sql/libsql.la \
	$(LIBDOVECOT)
test_dict_sql_DEPENDENCIES = \
	$(noinst_LTLIBRARIES) \
	../lib-sql/libdriver_test.la \
	../lib-sql/libsql.la \
	$(LIBDOVECOT_DEPS)

check-local:
	for bin in $(test_programs) $(check_PROGRAMS); do \
	  if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \
	done
